アプリ開発コース 5
◆ランダムな塗りつぶしで模写
このコースを始める前に、何らかのサンプル画像を用意しておくこと!
Step 1. 画像にランダムに書き込む
code:cpp
# include <Siv3D.hpp>
double Diff(const Image& a, const Image& b)
{
double d = 0.0;
for (auto itA = a.begin(), itB = b.begin(); itA != a.end(); ++itA, ++itB)
{
d += AbsDiff(itA->r, itB->r) + AbsDiff(itA->g, itB->g) + AbsDiff(itA->b, itB->b);
}
return d;
}
void Main()
{
const Image target = Dialog::OpenImage().fit(Window::Size());
Image image(target.size(), Palette::White);
Image old = image;
DynamicTexture texture(old);
double d1 = Diff(target, image);
while (System::Update())
{
for (int32 i = 0; i < 100; ++i)
{
const Point pos = RandomPoint(image.width(), image.height());
const ColorF color(Random(), Random(), Random(), Random());
const int32 size = Random(1, 10);
Circle(pos, size).paint(image, color);
const double d2 = Diff(target, image);
if (d2 < d1)
{
d1 = d2;
old = image;
}
else
{
image = old;
}
}
texture.fill(image);
texture.draw();
if (SimpleGUI::Button(U"Save", Vec2(520, 430), 100))
{
image.saveWithDialog();
}
}
}
Step 2. 応用
書き込む図形、色を変更してみる